package org.lq.shopping.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.log4j.Logger;
import org.lq.shopping.dao.Shop_judgeDao;
import org.lq.shopping.entity.Shop_judge;
import org.lq.shopping.util.DruidUtil;

/**
 * 评价晒单实现类
 * @author 王涛
 *
 * @时间 2018年9月26日
 * EShop
 */
public class Shop_judgeDaoImpl implements Shop_judgeDao {

	QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
	Logger log = Logger.getLogger(Shop_judgeDaoImpl.class);
	
	@Override
	public int add(Shop_judge t) {
		String sql = "insert into Shop_judge values(?,?,?,?,?,?,?)";
		int num = 0;
		try {
			num = qr.execute(sql, t.getOrder_id(),t.getUser_id(),t.getContent(),t.getImges(),t.getJudge_date(),t.getAdd_content(),t.getAdd_date());
		} catch (SQLException e) {
			log.error(e);
		}
		return num;
	}

	@Override
	public int delete(int id) {
		String sql = "delete from Shop_judge where order_id = ?";
		int num = 0;
		try {
			num = qr.execute(sql, id);
		} catch (SQLException e) {
			log.error(e);
		}
		return num;
	}

	@Override
	public int update(Shop_judge t) {
		String sql = "update Shop_judge set user_id = ?,content = ?,images = ?,judge_date = ?,add_content = ?,add_date = ? where order_id = ?";
		int num = 0;
		try {
			num = qr.execute(sql, t.getUser_id(),t.getContent(),t.getImges(),t.getJudge_date(),t.getAdd_content(),t.getAdd_date(),t.getOrder_id());
		} catch (SQLException e) {
			log.error(e);
		}
		return num;
	}

	@Override
	public List<Shop_judge> findAll() {
		// TODO 暂不需要实现
		return null;
	}

	@Override
	public Shop_judge findById(int id) {
		String sql = "select * from Shop_judge";
		Shop_judge shop_judge = null;
		try {
			shop_judge = qr.query(sql, new BeanHandler<>(Shop_judge.class),id);
		} catch (SQLException e) {
			log.error(e);
		}
		return shop_judge;
	}

	@Override
	public List<Shop_judge> LikeColumnByValue(String columName, Object value) {
		// TODO 暂不需要实现
		return null;
	}

	@Override
	public int getCount() {
		String sql = "select count(1) from Shop_judge";
		int num = 0;
		try {
			num = qr.query(sql, new ScalarHandler<BigDecimal>()).intValue();
		} catch (SQLException e) {
			log.error(e);
		}
		return num;
	}

	@Override
	public List<Shop_judge> findPage(int pageIndex, int pageSize) {
		String sql = "select * from "
				+ "(select rownum r,s.* from "
				+ "(select * from Shop_judge order by judge_date) s "
				+ "where rownum<=?)"
				+ "where r>?";
		int max = pageIndex*pageSize;
		int min = (pageIndex-1)*pageSize;
		List<Shop_judge> list = null;
		try {
			list = qr.query(sql, new BeanListHandler<>(Shop_judge.class),max,min);
		} catch (SQLException e) {
			log.error(e);
		}
		return list;
	}

}
